package Servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import tiendavirtual.exception.DAOExcepcion;
import tiendavirtual.modelo.Categoria;
import tiendavirtual.modelo.Publicacion;
import tiendavirtual.modelo.Usuario;
import tiendavirtual.negocio.GestionPublicacion;

/**
 * Servlet implementation class Servlet_BuscarPublicacion
 */
@WebServlet("/Servlet_BuscarPublicacion")
public class Servlet_BuscarPublicacion extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Servlet_BuscarPublicacion() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		

        String buscar_titulo = request.getParameter("txttitulo");
        
		GestionPublicacion negocio = new GestionPublicacion();

		
		try {
			
			Collection<Publicacion> p = new ArrayList<Publicacion>();
			p = negocio.buscar(buscar_titulo);
			
			
			if (p.isEmpty()==true){
				RequestDispatcher rd = request.getRequestDispatcher("no_hay_producto.jsp");
				rd.forward(request, response);	
			}else{
				request.setAttribute("PUBLICACIONES_ENCONTRADAS", p);
				
				RequestDispatcher rd = request.getRequestDispatcher("BuscarPublicacion.jsp");
				rd.forward(request, response);
			}
					
			
			
		} catch (DAOExcepcion e) {
			RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
			rd.forward(request, response);
		}
		
	

		
	}

}
